﻿
namespace Trentacular.Office.Server.Search.FacetedSearch.Providers
{
    /// <summary>
    /// Serves as a generic base class that is inherited by each spcific
    /// <see cref="ManagedPropertyFacetBuilder" /> implementation.
    /// </summary>
    /// <typeparam name="T">The field type of the managed property value.</typeparam>
    public abstract class BaseManagedPropertyFacetBuilder<T>
        : IManagedPropertyFacetBuilder
    {
        #region IManagedPropertyFacetBuilder Members

        /// <summary>
        /// Gets or sets the faceted search context.
        /// </summary>
        /// <value>The faceted search context.</value>
        public FacetedSearchContext FacetedSearchContext { get; set; }

        /// <summary>
        /// Gets or sets the facet provider configuration.
        /// </summary>
        /// <value>The facet provider configuration.</value>
        public FacetProviderConfiguration Configuration { get; set; }

        /// <summary>
        /// Gets or sets the name of the managed property.
        /// </summary>
        /// <value>The name of the managed property.</value>
        public string ManagedPropertyName { get; set; }

        /// <summary>
        /// Indicating whether the <see cref="ManagedPropertyFacetProvider"/> should
        /// sort the resulting facets by their keys.
        /// </summary>
        /// <value>
        /// 	<c>true</c> if the <see cref="ManagedPropertyFacetProvider"/>
        /// should sort the resulting facets by their keys; otherwise, <c>false</c>.
        /// </value>
        public abstract bool SortByKey { get; }

        /// <summary>
        /// Gets the facet keys for a single search result's managed property value.
        /// </summary>
        /// <param name="value">The facet keys for a single search result's managed
        /// property value.</param>
        /// <returns>An array of keys.</returns>
        public string[] GetKeys(object value)
        {
            return GetKeys((T)value);
        }

        /// <summary>
        /// Creates a facet for a given facet key and managed property value.
        /// </summary>
        /// <param name="key">The facet's key.</param>
        /// <param name="value">The managed property value.</param>
        /// <returns>The generated facet.</returns>
        public Facet CreateFacet(string key, object value)
        {
            return CreateFacet(key, (T)value);
        }

        #endregion

        /// <summary>
        /// Gets the facet keys for a single search result's managed property value.
        /// </summary>
        /// <param name="value">The facet keys for a single search result's managed
        /// property value.</param>
        /// <returns>An array of keys.</returns>
        public abstract string[] GetKeys(T value);

        /// <summary>
        /// Creates a facet for a given facet key and managed property value.
        /// </summary>
        /// <param name="key">The facet's key.</param>
        /// <param name="value">The managed property value.</param>
        /// <returns>The generated facet.</returns>
        public abstract Facet CreateFacet(string key, T value);
    }
}
